package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.StorehouseConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.StorehouseDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Storehouse;
import com.neusoft.neuiotms.dcam.fixed.model.enums.StorehouseStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.StorehouseRepository;
import com.neusoft.neuiotms.dcam.fixed.service.StorehouseService;

@Service
public class StorehouseServiceImpl implements StorehouseService {

    @Autowired
    private StorehouseRepository storehouseRepository;

    @Autowired
    private StorehouseConverter storehouseConverter;

    @Override
    public StorehouseDTO create(final StorehouseDTO dto) {
        final List<Storehouse> oldModels = this.storehouseRepository.getByName(dto.getName());
        if (CollectionUtils.isNotEmpty(oldModels)) {
            throw new UnitedException(ExceptionConstants.NAME_CAN_NOT_DUPLICATE, new Object[] {dto.getName() });
        }

        final Storehouse model = this.storehouseConverter.toModel(dto);
        this.storehouseRepository.save(model);
        return this.storehouseConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        try {
            this.storehouseRepository.deleteById(id);
        } catch (final Exception e) {
            throw new UnitedException("此库房还有备品备件，不能删除", new Object[] {id });
        }
    }

    @Override
    public StorehouseDTO update(final Long id, final StorehouseDTO dto) {
        Storehouse model;
        try {
            model = this.storehouseRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.storehouseConverter.toModel(dto);
        this.storehouseRepository.save(model);

        return this.storehouseConverter.toDTO(model);
    }

    @Override
    public List<StorehouseDTO> findAll() {
        final List<Storehouse> models = this.storehouseRepository.findAll();
        return this.storehouseConverter.toListDTO(models);
    }

    @Override
    public StorehouseDTO details(final Long id) {
        final Storehouse model;
        try {
            model = this.storehouseRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.storehouseConverter.toDTO(model);
    }

    @Override
    public Page<StorehouseDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<Storehouse> result = this.storehouseRepository.search(pageable, searchable);
        return this.storehouseConverter.toPageDTO(result);
    }

    @Override
    public void statusChange(final Long id, final String flag) {
        final Storehouse model;

        try {
            model = this.storehouseRepository.getOne(id);
        } catch (final Exception e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        // 状态
        if (flag.equals(StorehouseStatus.enable.toString())) {
            model.setStatus(StorehouseStatus.enable);
        }
        if (flag.equals(StorehouseStatus.disable.toString())) {
            model.setStatus(StorehouseStatus.disable);
        }
        if (flag.equals(StorehouseStatus.discard.toString())) {
            model.setStatus(StorehouseStatus.discard);
        }
        this.storehouseRepository.save(model);
    }

    @Override
    public Boolean LocationIsStorehouse(final String locationCode) {
        Boolean isStorehouse = false;
        if (null != locationCode) {
            final List<Storehouse> list = this.storehouseRepository.findByLocation_code(locationCode);
            isStorehouse = !list.isEmpty();
        }
        return isStorehouse;
    }

}
